<?php
class ModelSaleCouponCustomer extends Model {
	public function addCouponCustomer($customer_id, $coupon_rule, $coupon_rule_desc) {
		$query = $this->db->query("SELECT SUBSTRING(UUID(), 1, 8) AS coupon_code");
		$coupon_code = $query->row['coupon_code'];
		
      	$this->db->query("INSERT INTO " . DB_PREFIX . "coupon_customer SET customer_id = '" . (int)$customer_id . 
      		"', `name` = '" . $coupon_rule['name'] . 
      		"', `code` = '" . $coupon_code . 
      		"', discount_type = '" . (int)$coupon_rule['discount_type'] . 
      		"', discount_values = '" . $coupon_rule['discount_values'] . 
      		"', discount_points = '" . $coupon_rule['discount_points'] . 
      		"', max_use_time = '" . (int)$coupon_rule['max_use_time'] . 
      		"', date_start = UNIX_TIMESTAMP(CURDATE())" . 
      		", date_end = UNIX_TIMESTAMP(ADDDATE(CURDATE(), INTERVAL " . (int)$coupon_rule['max_use_day'] . " DAY))" . 
      		", category_ids = '" . $coupon_rule['category_ids'] . "'");
		$coupon_customer_id = $this->db->getLastId();
		
      	foreach ($coupon_rule_desc as $language_id => $value) {
      		$sql = "INSERT INTO " . DB_PREFIX . "coupon_customer_description SET coupon_customer_id = '" . (int)$coupon_customer_id . 
        		"', language_id = '" . (int)$language_id . 
        		"', display_name = '" . $value['display_name'] . 
        		"', description = '" . $value['description'] . "'";
			$this->db->query($sql);
      	}
      	return $coupon_code;
	}
	
	public function getCouponCustomer($coupon_customer_id) {
      	$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "coupon_customer WHERE coupon_customer_id = '" . (int)$coupon_customer_id . "'");
		return $query->row;
	}
	
	public function getCouponCustomers($data = array()) {
		$sql = "SELECT c.coupon_customer_id, c.`name`, c.`code`, c.discount_type, c.discount_values, c.discount_points, c.max_use_time, " . 
			"FROM_UNIXTIME(c.date_start, '%Y-%m-%d') date_start, FROM_UNIXTIME(c.date_end, '%Y-%m-%d') date_end, c.category_ids, " . 
			"CONCAT(m.firstname, ' ', m.lastname) customer_name, m.email FROM " . 
			DB_PREFIX . "coupon_customer c LEFT JOIN `" . 
			DB_PREFIX . "customer` m ON c.customer_id = m.customer_id";
		
		$sql_params = '';
		if (isset($data['filter_customer_id'])) {
			$sql_params .= " AND c.customer_id = '" . $data['filter_customer_id'] . "'";	
		}
		if (isset($data['filter_code'])) {
			$sql_params .= " AND c.`code` LIKE '" . $data['filter_code'] . "%'";	
		}
		if (isset($data['filter_name'])) {
			$sql_params .= " AND c.`name` LIKE '" . $data['filter_name'] . "%'";	
		}
		if (isset($data['filter_email'])) {
			$sql_params .= " AND m.email LIKE '" . $data['filter_email'] . "%'";	
		}
		if($sql_params != ''){
			$sql_params = trim($sql_params);
			$sql_params = ltrim($sql_params, "AND");
			$sql .= " WHERE " . $sql_params;
		}
		
		$sort_data = array(
			'c.code',
			'c.max_use_time',
			'c.date_start',
			'c.date_end'
		);	
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY c.max_use_time";	
		}
		if (isset($data['order']) && ($data['order'] == 'ASC')) {
			$sql .= " ASC";
		} else {
			$sql .= " DESC";
		}
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}		
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function getTotalCouponCustomers() {
      	$sql = "SELECT COUNT(*) AS total FROM " . 
			DB_PREFIX . "coupon_customer c LEFT JOIN `" . 
			DB_PREFIX . "customer` m ON c.customer_id = m.customer_id";
		
		$sql_params = '';
		if (isset($data['filter_customer_id'])) {
			$sql_params .= " AND c.customer_id = '" . $data['filter_customer_id'] . "'";	
		}
		if (isset($data['filter_code'])) {
			$sql_params .= " AND c.`code` LIKE '" . $data['filter_code'] . "%'";	
		}
		if (isset($data['filter_name'])) {
			$sql_params .= " AND c.`name` LIKE '" . $data['filter_name'] . "%'";	
		}
		if (isset($data['filter_email'])) {
			$sql_params .= " AND m.email LIKE '" . $data['filter_email'] . "%'";	
		}
		if($sql_params != ''){
			$sql_params = trim($sql_params);
			$sql_params = ltrim($sql_params, "AND");
			$sql .= " WHERE " . $sql_params;
		}
		
		$query = $this->db->query($sql);
		
		return $query->row['total'];
	}

	public function getCouponCustomerByCode($customer_id, $coupon_code) {
		$sql = "SELECT c.*, cd.display_name, cd.description, m.firstname, m.lastname, m.email, m.sex FROM " . 
			DB_PREFIX . "coupon_customer c LEFT JOIN " . 
			DB_PREFIX . "coupon_customer_description cd ON c.coupon_customer_id = cd.coupon_customer_id AND cd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "' LEFT JOIN `" . 
			DB_PREFIX . "customer` m ON c.customer_id = m.customer_id WHERE c.customer_id = '" . (int)$customer_id . 
			"' AND c.`code` = '" . $coupon_code . "'";
		$query = $this->db->query($sql);
		return $query->row;
	}
}
?>